#!/bin/bash
exec scala "$0" "$@"
!#

import java.lang.Math.log
import scala.io.Source.stdin
import scala.io.Source.fromFile
import scala.collection.mutable.Map

for (line <- stdin.getLines()) {
    var tag : String = "O"
    var begin = false
    for (word <- line.split(" ") if word != "") { // more than one space, or an empty line, is skipped
        if (word.matches("""\[[A-Z]+""")) {
            if (tag != "O") {
                System.err.println(" *** WARNING *** : Nested tags in Illinois NER output.\nLine = "+line+"\nSuggestion: you may want to remove any [ or ] from the input.")
            }
            tag = word.tail
            begin = true
        } else if(!word.endsWith("]") || word.size == 1) {
            if(tag != "O") {
                println(word + "\t" + (if (begin) { "B-" } else { "I-" }) + tag)
            } else {
                println(word + "\t" + tag)
            }
            begin = false
        } else {
            if (tag == "O") {
                System.err.println(" *** WARNING *** : Closing bracket outside of tag in Illinois NER output.\nLine = "+line+"\nSuggestion: you may want to remove any [ or ] from the input.")
                println(word.slice(0,word.size-1) + "\tO")
            } else {
                println(word.slice(0,word.size-1) + "\t" + (if (begin) { "B-" } else { "I-" }) + tag)
            }
            tag = "O"
        }
    }
    print("\n")
}

